package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.query.CustomerReportQueryObject;
import cn.wolfcode.crm.service.ICustomerReportService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") CustomerReportQueryObject qo){
        PageInfo<Map> pageResult = customerReportService.query(qo);
        model.addAttribute("result",pageResult);
        return "customerReport/list";
    }

    @RequestMapping("/listBar")
    public String listBar(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        List<Map> maps = customerReportService.listAll(qo);
        ArrayList<String> xList = new ArrayList<>();
        ArrayList<Integer> yList = new ArrayList<>();
        for (Map map : maps) {
            xList.add(map.get("groupType").toString());
            yList.add(Integer.valueOf(map.get("number").toString()));
        }
        model.addAttribute("xList",JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        return "customerReport/listBar";
    }

    @RequestMapping("/listPie")
    public String listPie(Model model,@ModelAttribute("qo") CustomerReportQueryObject qo){
        List<Map> mapList = customerReportService.listAll(qo);
        ArrayList<String> legend = new ArrayList<>();
        ArrayList<Map> series = new ArrayList<>();
        for (Map map : mapList) {
            legend.add(map.get("groupType").toString());
            HashMap<String, Object> maps = new HashMap<>();
            maps.put("value",map.get("number"));
            maps.put("name",map.get("groupType"));
            series.add(maps);
        }
        model.addAttribute("legend",JSON.toJSONString(legend));
        model.addAttribute("series",JSON.toJSONString(series));
        return "customerReport/listPie";
    }


  
}
